﻿using System;
using System.Collections.Generic;
using Sharp.DataTransferObject.CMS;
using Sharp.Domain.CMSBoundedContext.OperationModule.Aggregates;
using Sharp.Infrastructure;
using Sharp.Infrastructure.Search;

namespace Sharp.Application.CMS
{
    public partial class MemberServiceImpl
    {
        #region NoiseWord

        public Result DeleteNoiseWords(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "参数不可为空。";
                return res;
            }

            _noiseWordRepository.Remove(ids);

            Context.Commit();

            return res;
        }

        public Result SaveNoiseWord(NoiseWordDTO dtoModel)
        {
            Result res = new Result();
            if (dtoModel.Id.IsEmpty())
            {
                _noiseWordRepository.Add(dtoModel.To<NoiseWord>());
            }
            else
            {
                var persisted = _noiseWordRepository.Get(dtoModel.Id);
                persisted.Noise = dtoModel.Noise;
                persisted.Replacement = dtoModel.Replacement;
                persisted.IsBan = dtoModel.IsBan;

                _noiseWordRepository.Update(persisted);
            }
            _noiseWordRepository.Context.Commit();
            return res;
        }

        public Result<NoiseWordDTO> GetNoiseWordInfo(Guid id)
        {
            var res = new Result<NoiseWordDTO>();
            res.Data = _noiseWordRepository.Get(id).To<NoiseWordDTO>();
            return res;
        }

        public Result<IEnumerable<NoiseWordDTO>> GetNoiseWordList()
        {
            var res = new Result<IEnumerable<NoiseWordDTO>>();
            res.Data = _noiseWordRepository.GetList().To<NoiseWord, NoiseWordDTO>();
            return res;
        }

        public PagedResult<NoiseWordDTO> GetNoiseWordPaged(PagingFeature pf)
        {
            var paged = _noiseWordRepository.Pagging(pf);

            return paged.To<NoiseWord, NoiseWordDTO>();
        }

        #endregion

        #region IPBlackList

        public Result DeleteIPBlackLists(Guid[] ids)
        {
            var res = new Result();
            if (ids == null)
            {
                res.Err = "参数不可为空。";
                return res;
            }

            _ipBlackListRepository.Remove(ids);

            Context.Commit();

            return res;
        }

        public Result SaveIPBlackList(IPBlackListDTO dtoModel)
        {
            Result res = new Result();
            if (dtoModel.Id.IsEmpty())
            {
                _ipBlackListRepository.Add(dtoModel.To<IPBlackList>());
            }
            else
            {
                var persisted = _ipBlackListRepository.Get(dtoModel.Id);
                persisted.SectorStart = dtoModel.SectorStart;
                persisted.SectorEnd = dtoModel.SectorEnd;
                persisted.IPStart = dtoModel.IPStart;
                persisted.IPEnd = dtoModel.IPEnd;

                _ipBlackListRepository.Update(persisted);
            }
            _ipBlackListRepository.Context.Commit();
            return res;
        }

        public Result<IPBlackListDTO> GetIPBlackListInfo(Guid id)
        {
            var res = new Result<IPBlackListDTO>();
            res.Data = _ipBlackListRepository.Get(id).To<IPBlackListDTO>();
            return res;
        }

        public Result<IEnumerable<IPBlackListDTO>> GetIPBlackListList()
        {
            var res = new Result<IEnumerable<IPBlackListDTO>>();
            res.Data = _ipBlackListRepository.GetList().To<IPBlackList, IPBlackListDTO>();
            return res;
        }

        public PagedResult<IPBlackListDTO> GetIPBlackListPaged(PagingFeature pf)
        {
            var paged = _ipBlackListRepository.Pagging(pf);

            return paged.To<IPBlackList, IPBlackListDTO>();
        }

        #endregion
    }
}
